#!/bin/sh
#make a ramdisk for embedded device

#Check the script is being run by root user
if [[ $EUID -ne 0 ]]; then
	echo "This script must be run as root" 1>&2
	exit 1
fi

ramdiskname=ramdisk.fs
ramdiskzip=ramdisk_ist_3.0.x.gz
filesysdir=/home/ist/workdir/dvsdk_1_30_01_41/filesys
curpath=$PWD
mntdir=$PWD/mountpiont
des_dir=~/tftpboot

####delete.svn directory####
echo "#####Enter $filesysdir .."
echo "#####Delete trash .."
cd $filesysdir
find ./ -name .svn -exec rm -rf {} \;
echo "#####Leaving $filesysdir"

cd $curpath
if [ $filesysdir=$PWD ]; then
	echo "####Can't do this in:: $curpath "
	echo "###################"
fi

echo "#####Makeing file system..."
[ -e $ramdiskname ]  && rm -f $ramdiskname
dd if=/dev/zero of=$ramdiskname bs=1k count=30000
mke2fs -F -v -m 0 $ramdiskname
if [ -d $mntdir ]; then
	echo "#####$mntdir exist!"
else
	echo "#####Create $mntdir directory..."
	mkdir -p $mntdir
fi
mount -t ext2 -o loop $ramdiskname $mntdir
cp -aR ${filesysdir}/* ${mntdir}/
chown -R root.root ${mntdir}/*
umount $mntdir
gzip -c -9 $ramdiskname > $ramdiskzip
echo "#####Encrypt filesystem..."
### encrypt filesystem
/bin/en_decrypt_fs $ramdiskzip e 1>/dev/null

echo "#####Install filesystem to $des_dir..."
### install to tftp root directory
mv -f $ramdiskzip $des_dir

echo "#####Cleaning up ....."
## clean file
rm -rf $ramdiskname
rm -rf $mntdir
ls -al $des_dir/$ramdiskzip 1>&2
echo "#####Finished#####"
